/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eyeofthetiger.utils;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.Barcode;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.Barcode39;
import com.itextpdf.text.pdf.BarcodeCodabar;
import com.itextpdf.text.pdf.BarcodeDatamatrix;
import com.itextpdf.text.pdf.BarcodeEAN;
import com.itextpdf.text.pdf.BarcodeEANSUPP;
import com.itextpdf.text.pdf.BarcodeInter25;
import com.itextpdf.text.pdf.BarcodePDF417;
import com.itextpdf.text.pdf.BarcodePostnet;
import com.itextpdf.text.pdf.BarcodeQRCode;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import eyeofthetiger.model.Participant;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import org.jpedal.PdfDecoder;
import org.jpedal.examples.simpleviewer.SimpleViewer;
import org.jpedal.exception.PdfException;
import org.jpedal.fonts.FontMappings;

/**
 *
 * @author christophe
 */
public class PDFUtils {
   
    /*
    public static void test() {
        try {
            String fileName = "c:\\TEMP\\fdfz.pdf";
            createPdf(fileName);
            //OpenViewer(fileName);  
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }*/
   
    public static void OpenViewer(String fileName) {
        SimpleViewer sv = new SimpleViewer(fileName);
        sv.setupViewer();
    }
   
public static void createPdf(String filename) throws IOException, DocumentException {
        // step 1
        Document document = new Document(new Rectangle(340, 842));
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();
 
        String code = "2846510";
       
        // EAN 13
        document.add(new Paragraph("Barcode EAN.UCC-13"));
        BarcodeEAN codeEAN = new BarcodeEAN();
        /*
        //codeEAN.setCode("4512345678906");
        codeEAN.setCode(code);
        document.add(new Paragraph("default:"));
        document.add(codeEAN.createImageWithBarcode(cb, null, null));
        codeEAN.setGuardBars(false);
        document.add(new Paragraph("without guard bars:"));
        document.add(codeEAN.createImageWithBarcode(cb, null, null));
        codeEAN.setBaseline(-1f);
        codeEAN.setGuardBars(true);
        document.add(new Paragraph("text above:"));
        document.add(codeEAN.createImageWithBarcode(cb, null, null));
        codeEAN.setBaseline(codeEAN.getSize());

               
        // UPC A
        document.add(new Paragraph("Barcode UCC-12 (UPC-A)"));
        codeEAN.setCodeType(Barcode.UPCA);
        //codeEAN.setCode("785342304749");
        codeEAN.setCode(code);
        document.add(codeEAN.createImageWithBarcode(cb, null, null));

       
        // EAN 8
        document.add(new Paragraph("Barcode EAN.UCC-8"));
        codeEAN.setCodeType(Barcode.EAN8);
        codeEAN.setBarHeight(codeEAN.getSize() * 1.5f);
        //codeEAN.setCode("34569870");
        codeEAN.setCode(code);
        document.add(codeEAN.createImageWithBarcode(cb, null, null));
       
       
        // UPC E
        document.add(new Paragraph("Barcode UPC-E"));
        codeEAN.setCodeType(Barcode.UPCE);
        //codeEAN.setCode("03456781");
        codeEAN.setCode(code);
        document.add(codeEAN.createImageWithBarcode(cb, null, null));
        codeEAN.setBarHeight(codeEAN.getSize() * 3f);
       

        // EANSUPP
        document.add(new Paragraph("Bookland"));
        document.add(new Paragraph("ISBN 0-321-30474-8"));
        codeEAN.setCodeType(Barcode.EAN13);
        //codeEAN.setCode("9781935182610");
        codeEAN.setCode(code);
         
        BarcodeEAN codeSUPP = new BarcodeEAN();
        codeSUPP.setCodeType(Barcode.SUPP5);
        //codeSUPP.setCode("55999");
        codeSUPP.setCode(code);
        codeSUPP.setBaseline(-2);
        BarcodeEANSUPP eanSupp = new BarcodeEANSUPP(codeEAN, codeSUPP);
        document.add(eanSupp.createImageWithBarcode(cb, null, BaseColor.BLUE));
        */
       
        // CODE 128
        document.add(new Paragraph("Barcode 128"));
        Barcode128 code128 = new Barcode128();
        //code128.setCode("0123456789 hello");
        code128.setCode(code);
        document.add(code128.createImageWithBarcode(cb, null, null));
        code128.setCode("0123456789\uffffMy Raw Barcode (0 - 9)");
        code128.setCodeType(Barcode.CODE128_RAW);
        document.add(code128.createImageWithBarcode(cb, null, null));
 
        // Data for the barcode :
        String code402 = "24132399420058289";
        String code90 = "3700000050";
        String code421 = "422356";
        StringBuffer data = new StringBuffer(code402);
        data.append(Barcode128.FNC1);
        data.append(code90);
        data.append(Barcode128.FNC1);
        data.append(code421);
        Barcode128 shipBarCode = new Barcode128();
        shipBarCode.setX(0.75f);
        shipBarCode.setN(1.5f);
        shipBarCode.setSize(10f);
        shipBarCode.setTextAlignment(Element.ALIGN_CENTER);
        shipBarCode.setBaseline(10f);
        shipBarCode.setBarHeight(50f);
        shipBarCode.setCode(data.toString());
        document.add(shipBarCode.createImageWithBarcode(cb, BaseColor.BLACK,
                BaseColor.BLUE));
 
        // it is composed of 3 blocks whith AI 01, 3101 and 10
        Barcode128 uccEan128 = new Barcode128();
        uccEan128.setCodeType(Barcode.CODE128_UCC);
        uccEan128.setCode("(01)00000090311314(10)ABC123(15)060916");
        document.add(uccEan128.createImageWithBarcode(cb, BaseColor.BLUE,
                BaseColor.BLACK));
        uccEan128.setCode("0191234567890121310100035510ABC123");
        document.add(uccEan128.createImageWithBarcode(cb, BaseColor.BLUE,
                BaseColor.RED));
        uccEan128.setCode("(01)28880123456788");
        document.add(uccEan128.createImageWithBarcode(cb, BaseColor.BLUE,
                BaseColor.BLACK));
 
        // INTER25
        document.add(new Paragraph("Barcode Interleaved 2 of 5"));
        BarcodeInter25 code25 = new BarcodeInter25();
        code25.setGenerateChecksum(true);
        code25.setCode("41-1200076041-001");
        document.add(code25.createImageWithBarcode(cb, null, null));
        code25.setCode("411200076041001");
        document.add(code25.createImageWithBarcode(cb, null, null));
        code25.setCode("0611012345678");
        code25.setChecksumText(true);
        document.add(code25.createImageWithBarcode(cb, null, null));
 
        // POSTNET
        document.add(new Paragraph("Barcode Postnet"));
        BarcodePostnet codePost = new BarcodePostnet();
        document.add(new Paragraph("ZIP"));
        codePost.setCode("01234");
        document.add(codePost.createImageWithBarcode(cb, null, null));
        document.add(new Paragraph("ZIP+4"));
        codePost.setCode("012345678");
        document.add(codePost.createImageWithBarcode(cb, null, null));
        document.add(new Paragraph("ZIP+4 and dp"));
        codePost.setCode("01234567890");
        document.add(codePost.createImageWithBarcode(cb, null, null));
 
        document.add(new Paragraph("Barcode Planet"));
        BarcodePostnet codePlanet = new BarcodePostnet();
        codePlanet.setCode("01234567890");
        codePlanet.setCodeType(Barcode.PLANET);
        document.add(codePlanet.createImageWithBarcode(cb, null, null));
 
        // CODE 39
        document.add(new Paragraph("Barcode 3 of 9"));
        Barcode39 code39 = new Barcode39();
        code39.setCode("ITEXT IN ACTION");
        document.add(code39.createImageWithBarcode(cb, null, null));
 
        document.add(new Paragraph("Barcode 3 of 9 extended"));
        Barcode39 code39ext = new Barcode39();
        code39ext.setCode("iText in Action");
        code39ext.setStartStopText(false);
        code39ext.setExtended(true);
        document.add(code39ext.createImageWithBarcode(cb, null, null));
 
        // CODABAR
        document.add(new Paragraph("Codabar"));
        BarcodeCodabar codabar = new BarcodeCodabar();
        codabar.setCode("A123A");
        codabar.setStartStopText(true);
        document.add(codabar.createImageWithBarcode(cb, null, null));
 
        // PDF417
        document.add(new Paragraph("Barcode PDF417"));
        BarcodePDF417 pdf417 = new BarcodePDF417();
        String text = "Call me Ishmael. Some years ago--never mind how long "
            + "precisely --having little or no money in my purse, and nothing "
            + "particular to interest me on shore, I thought I would sail about "
            + "a little and see the watery part of the world.";
        pdf417.setText(text);
        Image img = pdf417.getImage();
        img.scalePercent(50, 50 * pdf417.getYHeight());
        document.add(img);
 
        document.add(new Paragraph("Barcode Datamatrix"));
        BarcodeDatamatrix datamatrix = new BarcodeDatamatrix();
        datamatrix.generate(text);
        img = datamatrix.createImage();
        document.add(img);
 
        document.add(new Paragraph("Barcode QRCode"));
        BarcodeQRCode qrcode = new BarcodeQRCode("Moby Dick by Herman Melville", 1, 1, null);
        img = qrcode.getImage();
        document.add(img);
 
        // step 5
        document.close();
    }    
   




    
    private static Paragraph createCleanParagraph(String txt1, float fontSize1, boolean bold1) {
        Phrase phrase = new Phrase(txt1);
        phrase.getFont().setSize(fontSize1);
        if(bold1) {
            phrase.getFont().setStyle(Font.BOLD);
            phrase.setLeading(fontSize1);  
        }
        Paragraph paragraph = new Paragraph(phrase);
        
        paragraph.setLeading(fontSize1);
        paragraph.setSpacingBefore(0);
        paragraph.setSpacingAfter(0);
        paragraph.setExtraParagraphSpace(0);
        paragraph.setFirstLineIndent(0);
        paragraph.setIndentationLeft(0);
        paragraph.setIndentationRight(0);
        paragraph.setAlignment(Rectangle.ALIGN_CENTER);
        
        return paragraph;
    }
    
    
    private static Element createCleanBarcode(PdfContentByte cb,String numero, float width,float height) {
        Barcode128 code128 = new Barcode128();
        code128.setCode(numero);
        Image barcodeImage = code128.createImageWithBarcode(cb, null, null);
        barcodeImage.setAlignment(Image.ALIGN_CENTER);
        //barcodeImage.setWidthPercentage(75f);
        barcodeImage.scaleToFit(width, height);
        return barcodeImage;        
    }
    
    

    public static void createPdf2(List<Participant> participants, boolean exportName, boolean exportGroup, boolean exportRenseignement, OutputStream out) throws IOException, DocumentException {
        Document document = new Document(PageSize.A4.rotate());
        //document.setMargins(0,0,0,0);
        PdfWriter writer = PdfWriter.getInstance(document, out);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        
        float documentTop = document.top();
        float documentBottom = document.bottom();
        float documentHeight = documentTop - documentBottom;
        float left = document.left();
        float right = document.right();
        float width = right - left;
        
        cb.rectangle(left, documentBottom, width, documentHeight);
        cb.stroke();
        
        float nameHeightPercent = 0.35f;
        float groupHeightPercent = 0.25f; 

        
        float nameTop = documentTop ;
        float nameBottom = nameTop;
        if(exportName) {
            nameBottom = nameTop - (documentHeight * nameHeightPercent);
        }
        float groupeTop = nameBottom;
        float groupeBottom = nameBottom;
        if(exportGroup) {
            groupeBottom = groupeTop - (documentHeight *  groupHeightPercent);
        }
        float barcodeTop = groupeBottom;
        float barcodeBottom = documentBottom;
        
        
        ColumnText columnText;
        
        for(Participant participant : participants) {

            float nameFontSize = 65f;
            float groupFontSize = 45f;
            float renseignementFontSize = 35f;
            
            if(exportName) {
                columnText = new ColumnText(cb);
                
                columnText.setSimpleColumn(left, nameTop, right, nameBottom);
                //cb.rectangle(left, nameBottom, width, (nameTop - nameBottom));
                //cb.stroke();
                
                columnText.setExtraParagraphSpace(0f);
                columnText.setAdjustFirstLine(false);
                columnText.setIndent(0);
                
                String txt = participant.getNom().toUpperCase() + " " + participant.getPrenom();
                
                float previousPos = columnText.getYLine();
                columnText.setText(null);
                columnText.addElement(createCleanParagraph(txt,nameFontSize,true));
                while(ColumnText.hasMoreText(columnText.go(true))) {
                    nameFontSize = nameFontSize - 0.5f;
                    columnText.setText(null);
                    columnText.addElement(createCleanParagraph(txt,nameFontSize,true));
                    columnText.setYLine(previousPos);
                } 
              
                columnText.setText(null);
                columnText.addElement(createCleanParagraph(txt,nameFontSize,true));
                columnText.setYLine(previousPos);
                columnText.go(false);

            }
            
           
            
            if(exportGroup) {
                columnText = new ColumnText(cb);

                columnText.setSimpleColumn(document.left(), groupeTop, document.right(), groupeBottom);
                float groupeHeight = groupeTop - groupeBottom;
                //cb.rectangle(document.left(), groupeTop - groupeHeight, document.right() - document.left(), groupeHeight);
                //cb.stroke();
                
                columnText.setExtraParagraphSpace(0f);
                columnText.setAdjustFirstLine(false);
                columnText.setIndent(0);
                columnText.setFollowingIndent(0);
                
                String txt1 = participant.getGroupe();
                String txt2 = exportRenseignement ? participant.getRenseignements() : null;
                
                float previousPos = columnText.getYLine();
                columnText.setText(null);
                //columnText.addElement(createCleanParagraph(txt1,groupFontSize,true,txt2,renseignementFontSize,false));
                columnText.addElement(createCleanParagraph(txt1,groupFontSize,true));
                columnText.addElement(createCleanParagraph(txt2,renseignementFontSize,false));
                while(ColumnText.hasMoreText(columnText.go(true))) {
                    groupFontSize = groupFontSize - 0.5f;
                    renseignementFontSize = renseignementFontSize - 0.5f;
                    columnText.setText(null);
                    //columnText.addElement(createCleanParagraph(txt1,groupFontSize,true,txt2,renseignementFontSize,false));
                    columnText.addElement(createCleanParagraph(txt1,groupFontSize,true));
                    columnText.addElement(createCleanParagraph(txt2,renseignementFontSize,false));                    
                    columnText.setYLine(previousPos);
                } 
              
                columnText.setText(null);
                //columnText.addElement(createCleanParagraph(txt1,groupFontSize,true,txt2,renseignementFontSize,false));
                columnText.addElement(createCleanParagraph(txt1,groupFontSize,true));
                columnText.addElement(createCleanParagraph(txt2,renseignementFontSize,false));                
                columnText.setYLine(previousPos);
                columnText.go(false);
            }
           
            
            {
                columnText = new ColumnText(cb);

                barcodeTop = barcodeTop - 12f;
                columnText.setSimpleColumn(left, barcodeTop, right, barcodeBottom);
                float barcodeHeight = barcodeTop - barcodeBottom;
                //cb.rectangle(left, barcodeTop - barcodeHeight, width, barcodeHeight);
                //cb.stroke();
                
                columnText.setExtraParagraphSpace(0f);
                columnText.setAdjustFirstLine(false);
                columnText.setIndent(0);
                
                float previousPos = columnText.getYLine();
                columnText.setText(null);
                columnText.addElement(createCleanBarcode(cb, participant.getNumero(),width,barcodeHeight));
                columnText.go(false);                
            }
            
            
            document.newPage();
                   
        }
       
        document.close();        
    }  





    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    public static BufferedImage PdfToImage(byte[] pdfFile) throws Exception {
        PdfDecoder decode_pdf = new PdfDecoder(true);
        /**set mappings for non-embedded fonts to use*/
        //FontMappings.setFontReplacements();

        /**open the PDF file - can also be a URL or a byte array*/
        try {
            decode_pdf.openPdfArray(pdfFile);

            decode_pdf.setExtractionMode(PdfDecoder.FINALIMAGES); //do not save images
            /**get page 1 as an image*/
            BufferedImage img=decode_pdf.getPageAsImage(1);
	
            /**close the pdf file*/
            decode_pdf.closePdfFile();
            return img;
        } catch (PdfException e) {
            throw new Exception(e.getMessage(),e);
        }
        
    }









   
}
